﻿using System;
using System.Collections.Generic;
using System.Linq;
using Api.DAL;
using System.Linq.Expressions;
using Api.Model;

namespace Api.BLL
{
    public abstract class BaseBLL<T> where T : BaseModel
    {
        public BaseBLL()
        {
            SetDAL();
        }

        protected BaseDAL<T> dal;
        protected virtual void SetDAL()
        {
            dal = BaseDAL<T>.Current;
        }


        public int Add(T model)
        {
            return dal.Insert(model);
        }

        public int Update(T model)
        {
            return dal.Update(model);
        }

        public int UpdateIgnore(Expression<Func<T, object>> propertyExpression, params T[] entities)
        {
            return dal.UpdateIgnore(propertyExpression, entities);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="originalEntity"></param>
        /// <param name="newEntity">只能使用匿名对象</param>
        /// <returns></returns>
        public int Update(T originalEntity, object newEntity)
        {
            return dal.Update(originalEntity, newEntity);
        }

        public int Delete(T model)
        {
            return dal.Delete(model);
        }

        public void AddList(IEnumerable<T> list)
        {
            dal.InsertAll(list);
        }

        public void UpdateAll(IEnumerable<T> list)
        {
            dal.UpdateAll(list);
        }

        public void DeleteList(IEnumerable<T> list)
        {
            dal.DeleteList(list);
        }

        public void DeleteList(Expression<Func<T, bool>> predicate)
        {
            dal.DeleteList(predicate);
        }

        public T GetModel(Expression<Func<T, bool>> predicate = null)
        {
            return dal.GetModel(predicate);
        }
        public IEnumerable<T> GetList(Expression<Func<T, bool>> predicate = null)
        {
            return dal.GetList(predicate, false);
        }

        public List<T> GetListNoTrack(Expression<Func<T, bool>> predicate = null)
        {
            //将数据实例化
            return dal.GetList(predicate, true).ToList();
        }
    }
}